//
//  MSLocationManager.m
//  MSLocationDemo
//
//  Created by iOS on 2017/5/22.
//  Copyright © 2017年 MS. All rights reserved.
//

#import "MSLocationManager.h"
#import <AMapFoundationKit/AMapFoundationKit.h>

static MSLocationManager *manager;

@interface MSLocationManager ()<AMapLocationManagerDelegate>

@property (nonatomic, strong) AMapLocationManager *locationManager;

@property (nonatomic, strong) UISegmentedControl *showSegment;

@end

@implementation MSLocationManager

+ (MSLocationManager *)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    
    return manager;
}

- (void)starUpdatingLocationWithHandle:(DidUpdateLocationBlock)block
{
    self.block = block;
    [self.locationManager startUpdatingLocation];
}

- (void)stopUpdatingLocation
{
    [self.locationManager stopUpdatingLocation];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [AMapServices sharedServices].apiKey = @"f3bda7109d2b9dcca4afe621b7bddc26";
        [self configLocationManager];
    }
    return self;
}

- (void)configLocationManager
{
    self.locationManager = [[AMapLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    
    //设置不允许系统暂停定位
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    //设置允许在后台定位
    //    [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    //设置允许连续定位逆地理
    [self.locationManager setLocatingWithReGeocode:YES];
}


- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    if (reGeocode.formattedAddress && reGeocode.formattedAddress.length > 1)
    {
        if (self.block)
        {
            self.block(location, reGeocode.formattedAddress);
        }
    }
}


@end
